package com.gimi.cloud.bbp.commonService.mq.config;

import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;

/**
 * 生产者配置
 *
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2017-12-26
 **/
@Configuration
public class ProducerConfig {
    @Bean
    RabbitAdmin rabbitAdmin() {
        return rabbitAdmin();
    }

    @Bean
    RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
        return new RabbitAdmin(connectionFactory);
    }

    /**
     * 生产者用
     * @return
     */
    @Bean
    public RabbitMessagingTemplate rabbitMessagingTemplate(RabbitTemplate rabbitTemplate) {
        RabbitMessagingTemplate rabbitMessagingTemplate = new RabbitMessagingTemplate();
        rabbitMessagingTemplate.setMessageConverter(fastJson2MessageCoverter());
        rabbitMessagingTemplate.setRabbitTemplate(rabbitTemplate);
        return rabbitMessagingTemplate;
    }

    @Bean
    public MappingFastJson2MessageCoverter fastJson2MessageCoverter(){
        MappingFastJson2MessageCoverter coverter = new MappingFastJson2MessageCoverter();
        return coverter;
    }
}
